import util.tool as tool
import re

summary = tool.read_file("data/摘要.md")


def split_markdown_by_heading(file_path, heading_level=3):
    delimiter = "#" * heading_level + " "
    current_section = []

    with open(file_path, "r", encoding="utf-8") as f:
        for line in f:
            if line.startswith(delimiter):
                if current_section:
                    yield "".join(current_section)
                    current_section = []
            current_section.append(line)

        if current_section:
            yield "".join(current_section)


# 使用示例
for section in split_markdown_by_heading("data/摘要.md"):
    print(section)


# 使用正则表达式匹配标题行
pattern = r"(^#+\s+.+?$)([\s\S]*?)(?=^#+\s+.+?$|\Z)"
matches = re.finditer(pattern, summary, flags=re.MULTILINE)

result = []
for match in matches:
    title = match.group(1).strip()
    body = match.group(2).strip()
    result.append(title + "\n" + body)

for item in result:
    print("=" * 20)
    print(item)


# 摘要分隔
tool.save_json("data/摘要分隔.json", result)
